package com.cn.xiaonuo.slbfinances.slbshopes.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.utils.DateUtils;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.shopassets.entity.SlbShop;
import com.cn.xiaonuo.slbfinances.slbshopes.entity.SlUserCommission;
import com.cn.xiaonuo.slbfinances.slbshopes.mapper.SlUserCommissionMapper;
import com.cn.xiaonuo.slbfinances.slbshopes.mapper.SlbShopMapper;
import com.cn.xiaonuo.slbfinances.slbshopes.service.ISlbShopService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SlbShopServiceImpl extends ServiceImpl<SlbShopMapper, SlbShop> implements ISlbShopService {
    @Resource
    private SlbShopMapper slbShopMapper;
    @Resource
    private SlUserCommissionMapper slUserCommissionMapper;

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage platformPaymentRecord(Integer pageNum, Integer pageSize, String realName, String phone, String status, Long startTime, Long endTime, String userId, Integer order_id) {
        BusinessMessage message = new BusinessMessage<>();
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime1 = "";
        String endTime1 = "";
        if (startTime != null && endTime != null) {
            startTime1 = sim.format(new Date(startTime));
            endTime1 = sim.format(new Date(endTime));
        }
        if (null == pageNum || pageNum <= 1) {
            pageNum = 1;
        }
        if (null == pageSize || pageSize <= 1) {
            pageSize = 10;
        }
        // 设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> list = slbShopMapper.platformPaymentRecord(realName,
                phone,
                status,
                startTime1,
                endTime1,
                userId,
                order_id);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        if (list.size() <= 0) {
            message.setSuccess(false);
            message.setMessage("没有查询到打款记录");
            return message;
        }
        Double sumRoyalty = slbShopMapper.platformPaymentRecordSumRoyalty(realName,
                phone,
                status,
                startTime1,
                endTime1,
                userId,
                order_id);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("sumRoyalty", sumRoyalty);
        resultMap.put("pageInfo", pageInfo);
        message.setMessage("查询数据成功");
        message.setSuccess(true);
        message.setData(resultMap);
        return message;
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage<Object> confirmationPayment(String id) throws Exception {
        BusinessMessage<Object> message = new BusinessMessage<>();
        SlUserCommission slUserCommission = slUserCommissionMapper.selectById(id);
        if(slUserCommission==null){
            message.setMessage("打款记录不存在");
            message.setSuccess(false);
            return message;
        }
        slUserCommission.setUpdateTime(DateUtils.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
        slUserCommission.setStatus(0);
        int i = slUserCommissionMapper.updateById(slUserCommission);
        if(i>0){
            message.setMessage("打款操作成功");
            message.setSuccess(true);
            return message;
        }
        message.setMessage("打款操作失败");
        message.setSuccess(false);
        return message;
    }

    /**
     * 获取会员信息
     * @param userId
     * @return
     */
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public SlbShop getByUserId(Integer userId){
        SlbShop  slbShop= slbShopMapper.getByUserId(userId);
        return slbShop;
    }
}
